package cvo.pro4.chess.client.entities;

import cvo.pro4.chess.client.exceptions.IllegalCoordinatesException;



public class Game {
	private static Game game;
	
	private Army whiteArmy, blackArmy;
	private State state;
	private Player playerOne, playerTwo;
	private Board board;

	
	/**
	 * When a new game is made one player is present and the state of the game is new
	 * @param player
	 * @throws IllegalCoordinatesException
	 */
	private Game(Player player) throws IllegalCoordinatesException {
		this.board = new Board(); //Game gets a board		
		this.state = State.NEW;
		this.playerOne = player;		
		this.playerOne.setArmy(this.board.getWhiteArmy());
		
	}
	
	/**
	 * Opens up a new game and gives a handle of the game
	 * @param player
	 * @return The game that started
	 * @throws IllegalCoordinatesException
	 */
	public static Game startNewGame(Player player) throws IllegalCoordinatesException{
		if(player != null && game == null){
			game = new Game(player);
			return game;
		}
		return null;
		
	}
	
	public static Game joinGame(Player player){
		if(game !=null && game.state.equals(State.NEW) && player != null){
			game.setPlayerTwo(player);
			game.state = State.STARTED;
			game.getPlayerOne().setArmy(game.getBoard().getBlackArmy());
		}
		return null;
	}
	

	/**
	 * Gives the started game otherwise null if game not started
	 * @param player
	 * @return
	 * @throws IllegalCoordinatesException
	 */
	public static Game getGame() throws IllegalCoordinatesException{
		if(game != null ){
			return game;
		}
		return null;
	}
	
	
	public Board getBoard(){
		return this.board;
	}
	
	public State getGameState() {
		return this.state;
	}

	public Army getWhiteArmy() {
		return whiteArmy;
	}

	public Army getBlackArmy() {
		return blackArmy;
	}

	public State getState() {
		return state;
	}

	public Player getPlayerOne() {
		return playerOne;
	}

	public Player getPlayerTwo() {
		return playerTwo;
	}

	public void setPlayerOne(Player playerOne) {
		this.playerOne = playerOne;
	}

	public void setPlayerTwo(Player playerTwo) {
		this.playerTwo = playerTwo;
	}
	
	

}
